package com.dataextend.config.shiro;

import com.dataextend.config.jwt.JwtFilter;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);  // 设置securityManager
        //拦截器
        /*
         * 配置shiro拦截器链
         *
         * anon  不需要认证
         * authc 需要认证
         * user  验证通过或RememberMe登录的都可以
         *
         * 当应用开启了rememberMe时,用户下次访问时可以是一个user,但不会是authc,因为authc是需要重新认证的
         *
         * 顺序从上到下,优先级依次降低
         *
         */
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/userprofile-platform", "anon");
        filterChainDefinitionMap.put("/api/user/login", "anon");  // 用户登录接口
        filterChainDefinitionMap.put("/api/user/logout", "anon");  // 用户退出登录接口
        filterChainDefinitionMap.put("/druid/**", "anon");  // 放行 druid 数据源监控页面不拦截

        filterChainDefinitionMap.put("/v1/UserProfile/userTagSearch", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/propertyAndAct/calculate", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/property/filter/list", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/analysis/dimension/editAndadd", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/analysis/dimension/list", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/analysis/list", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/propertyAndAct/getGroupId", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/dimension/analysis", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/list", "anon");
        filterChainDefinitionMap.put("/v1/UserGroup/123", "anon");
        filterChainDefinitionMap.put("/v1/property/list", "anon");

        filterChainDefinitionMap.put("/v1/Market/regular/mission/create", "anon");
        filterChainDefinitionMap.put("/regular/usergroup/list", "anon");
        filterChainDefinitionMap.put("/v1/Market/channel/list", "anon");
        filterChainDefinitionMap.put("/v1/Market/regular/mission/analysis", "anon");

        /*
         * 放行 swagger 页面
         * */
        filterChainDefinitionMap.put("/swagger**/**", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/swagger-ui.html#!/**", "anon");

        // 添加自己的过滤器并且取名为jwt
//        Map<String, Filter> filterMap = new HashMap<String, Filter>(1);
//        filterMap.put("jwt", new JwtFilter());
//        shiroFilterFactoryBean.setFilters(filterMap);
//        //<!-- 过滤链定义，从上向下顺序执行，一般将/**放在最为下边
//        filterChainDefinitionMap.put("/**", "jwt");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(ShiroRealm shiroRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        /*
         * 关闭shiro自带的session，详情见文档
         * http://shiro.apache.org/session-management.html#SessionManagement-StatelessApplications%28Sessionless%29
         */
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);

        securityManager.setRealm(shiroRealm);
        return securityManager;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
